<?php 
    define("OK",1);
    define("WRONG_USER",2);
    define("WRONG_PASS",3);
    define("ACCESS",4);
    class UserTableModel extends Table{
        function __construct()
        {
            parent::__construct('bg_users');
        }
        public function login($userName,$passWord){
            $passWord = md5($passWord);
            $data = "";
            $this->setQuery("SELECT user_pass,user_level,user_id,user_status FROM ".$this->getTable()." WHERE user_name = '".$userName."'");
            $dt = $this->getResult();
            $checkUser = $this->getRecords($dt);
            if($checkUser > 0){
                $values = $this->getAssoc();
                if($values[0]['user_pass'] == $passWord){
                    if($values[0]['user_id'] == 1){
                        $data = OK;
                    }else{
                        if($values[0]['user_level'] == 1 && $values[0]['user_status'] == 1)
                        {
                             $data = OK;
                        }else{
                            $data = ACCESS;
                        }
                    }
                }else{
                    $data = WRONG_PASS;
                }
            }else{
                $data = WRONG_USER;
            }
            return $data;
            
        }
        public function getInfoByUserName($userName){
            $where = array("user_name"=>$userName);
            $column = array("user_id","user_name","user_pass","user_level");
            return $this->select("assoc",$column,$where);
            
        }
        public function addUser(User $user){
            $column = array(
                            "user_name"=>$user->_user_name,
                            "user_pass"=>$user->_user_pass,
                            "user_email"=>$user->_user_email,
                            "user_url"=>$user->_user_url,
                            "user_createdate"=>$user->_user_createdate,
                            "user_activation_key"=>$user->_user_activation_key,
                            "user_status"=>$user->_user_status,
                            "user_display_name"=>$user->_user_display_name,
                            "user_level"=>$user->_user_level
                        );
            return $this->insert($column);
        }
        
        public function checkEmail($user_email){
            $this->setQuery("SELECT user_id FROM ".$this->getTable()." WHERE user_email='".$user_email."'");
            $dt = $this->getResult();
            return $this->getRecords($dt);
        }
        public function checkUserName($user_name){
            $this->setQuery("SELECT user_id FROM ".$this->getTable()." WHERE user_name='".$user_name."'");
            $dt = $this->getResult();
            return $this->getRecords($dt);
        }
        public function editUser($user_id,$user_name,$user_pass,$user_email,$user_url,$user_createdate,$user_status,$user_display_name,$user_level){
            $column = array(
                            "user_pass"=>$user_pass,
                            "user_email"=>$user_email,
                            "user_url"=>$user_url,
                            "user_createdate"=>$user_createdate,
                            "user_status"=>$user_status,
                            "user_display_name"=>$user_display_name,
                            "user_display_name"=>$user_level
                        );
            $where = array("user_id"=>$user_id);
            return $this->update($column);
        }
        public function selectUser($column,$orderBy,$condition=null){
            return $this->select("assoc",$column,$condition,$orderBy);
        }
        
    }
?>